package no.hib.mod250.chat.client.model;

import java.io.Serializable;
import java.util.ArrayList;

public class Model {

	@SuppressWarnings("serial")
	public static class UserAdmin implements Serializable {

		protected String id = "";
		protected String email = "";
		protected String nickname = "";
		protected boolean isLoggedIn = false;
		protected String urlLogin = "";
		protected String urlLogout = "";
		protected boolean isAdmin = false;

		public UserAdmin() {
		}

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public String getNickname() {
			return nickname;
		}

		public void setNickname(String nickname) {
			this.nickname = nickname;
		}

		public boolean isLoggedIn() {
			return isLoggedIn;
		}

		public void setLoggedIn(boolean isLoggedIn) {
			this.isLoggedIn = isLoggedIn;
		}

		public String getUrlLogin() {
			return urlLogin;
		}

		public void setUrlLogin(String urlLogin) {
			this.urlLogin = urlLogin;
		}

		public String getUrlLogout() {
			return urlLogout;
		}

		public void setUrlLogout(String urlLogout) {
			this.urlLogout = urlLogout;
		}

		public boolean isAdmin() {
			return isAdmin;
		}

		public void setAdmin(boolean isAdmin) {
			this.isAdmin = isAdmin;
		}

		public Model.User getEventObject() {
			Model.User user = new Model.User();
			user.setId(this.id);
			user.setEmail(this.email);
			user.setNickname(this.nickname);
			return user;
		}

	}

	@SuppressWarnings("serial")
	public static class User implements Serializable {

		public static final int STATE_AVAILABLE = 0, STATE_IDLE = 1,
				STATE_BUSY = 2, STATE_OFFLINE = 3;

		protected String id = "";
		protected String email = "";
		protected String nickname = "";
		protected long timestamp = 0L;
		protected int state = STATE_AVAILABLE;

		public User() {
		}

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public String getNickname() {
			return nickname;
		}

		public void setNickname(String nickname) {
			this.nickname = nickname;
		}

		public long getTimestamp() {
			return timestamp;
		}

		public void setTimestamp(long timestamp) {
			this.timestamp = timestamp;
		}

		public int getState() {
			return state;
		}

		public void setState(int state) {
			this.state = state;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj instanceof User) {
				return ((User) obj).getId().equalsIgnoreCase(this.id);
			}
			return false;
		}

		public String toString() {
			String str = "";
			str += "Id: " + this.id + " ";
			str += "Nick: " + this.nickname + " ";
			str += "State: " + this.state;
			return str;
		}

	}

	@SuppressWarnings("serial")
	public static class Message implements Serializable {

		protected long id = 0;
		protected User user = null;
		protected String message = "";
		protected long timestamp = 0;

		public Message() {
		}

		public Message(long id) {
			this.id = id;
		}

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}

		public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public long getTimestamp() {
			return timestamp;
		}

		public void setTimestamp(long timestamp) {
			this.timestamp = timestamp;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj instanceof Model.Message) {
				return ((Model.Message) obj).getId() == this.id;
			}
			return super.equals(obj);
		}

		public String toString() {
			return "#" + this.id + " " + this.timestamp + ": " + this.message
					+ " - " + this.user;
		}

	}

	@SuppressWarnings("serial")
	public static class MessageList extends ArrayList<Message> {
		/**
		 * @param messageId
		 * @return Message with given Id, null of not found
		 */
		public Message get(long messageId) {
			for (Message message : this) {
				if (message.getId() == messageId) {
					return message;
				}
			}
			return null;
		}

		/**
		 * @param messageId
		 * @return Removed Message, null of not found
		 */
		public Message remove(long messageId) {
			for (Message message : this) {
				if (message.getId() == messageId) {
					remove(message);
					return message;
				}
			}
			return null;
		}
	}

	@SuppressWarnings("serial")
	public static abstract class Action implements Serializable {

		public long timestamp;

		public long getTimestamp() {
			return timestamp;
		}

	}

	@SuppressWarnings("serial")
	public static class DeleteMessageAction extends Action {

		public long id;

		public DeleteMessageAction() {
		}

		public DeleteMessageAction(long timestamp, long id) {
			this.timestamp = timestamp;
			this.id = id;
		}

		public long getId() {
			return id;
		}

	}

	@SuppressWarnings("serial")
	public static class DeleteMessagesAction extends Action {

		public DeleteMessagesAction() {
		}

		public DeleteMessagesAction(long timestamp) {
			this.timestamp = timestamp;
		}

	}

}
